﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using KINLO.UI.Main.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls; 
using System.Collections.ObjectModel;
using KINLO.Model;
using KINLO.Data.Reporitory;
using System.Windows;

namespace KINLO.UI.Main.VM
{
    public class HistoryEventVM:ViewModelBase
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MainVM));
        HistoryEventView View;
        LogReporitory LogDB = new LogReporitory();
        public HistoryEventVM(HistoryEventView view)
        { 
            this.View = view;
            StartDate = DateTime.Now.AddMonths(-1);
            EndDate = DateTime.Now;
            EventLevel = "ALL";
        }

        DateTime startDate;
        public DateTime StartDate
        {
            get { return startDate; }
            set
            {
                startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }
        DateTime endtDate;
        public DateTime EndDate
        {
            get { return endtDate; }
            set
            {
                endtDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        string eventLevel;
        public string EventLevel
        {
            get { return eventLevel; }
            set
            {
                eventLevel = value;
                RaisePropertyChanged("EventLevel");
            }
        }
        //AllSelected

        bool allSelected;
        public bool AllSelected
        {
            get { return allSelected; }
            set
            {
                allSelected = value;
                RaisePropertyChanged("AllSelected");
            }
        }

        RelayCommand<DataGridRowEventArgs> cmdLoadingRow;
        public RelayCommand<DataGridRowEventArgs> CmdLoadingRow
        {
            get { return cmdLoadingRow ?? (cmdLoadingRow = new RelayCommand<DataGridRowEventArgs>((e) => DoCmdLoadingRow(e))); }
        }
        void DoCmdLoadingRow(DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
        ObservableCollection<HistoryEvent> historyEvents;
        public ObservableCollection<HistoryEvent> HistoryEvents
        {
            get { return historyEvents; }
            set
            {
                historyEvents = value;
                RaisePropertyChanged("HistoryEvents");
            }
        } 

        /// <summary>
        /// 事件选择
        /// </summary>
        RelayCommand cmdCbkSelect;
        public RelayCommand CmdCbkSelect
        {
            get { return cmdCbkSelect ?? (cmdCbkSelect = new RelayCommand(() => DoCmdCbkSelect())); }
        }
        void DoCmdCbkSelect()
        { 
            if (HistoryEvents != null && HistoryEvents.Count > 0)
            {
                foreach (var evt in HistoryEvents)
                {
                    evt.IsChecked = AllSelected;
                } 
            }
        }
        /// <summary>
        /// 事件查询
        /// </summary>
        RelayCommand cmdEventQuery;
        public RelayCommand CmdEventQuery
        {
            get { return cmdEventQuery ?? (cmdEventQuery = new RelayCommand(() => DoCmdEventQuery())); }
        }
        void DoCmdEventQuery( )
        {
           var historyEvent= LogDB.GetLogEventInfo(StartDate, EndDate,EventLevel);
            if(historyEvent != null)
            {
                HistoryEvents = new ObservableCollection<HistoryEvent>(historyEvent); 
            }
        }

        /// <summary>
        /// 事件清除
        /// </summary>
        RelayCommand cmdEventClear;
        public RelayCommand CmdEventClear
        {
            get { return cmdEventClear ?? (cmdEventClear = new RelayCommand(() => DoCmdEventClear())); }
        }
        void DoCmdEventClear()
        {
            try
            {
                if (MessageBox.Show("确定删除", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    //for (var i=0;i<HistoryEvents.Count;i++)
                    //{
                    //    var history = HistoryEvents[i];
                    //    if (history.IsChecked == true)
                    //    { 
                    //        LogDB.DeleteLogInfo(history.Id.ToString());
                    //    }                        
                    //}
                    StringBuilder sb = new StringBuilder();
                    foreach(var e in HistoryEvents)
                    {
                        if (e.IsChecked)
                            sb.Append(e.Id + ",");
                    }
                    if (!string.IsNullOrEmpty(sb.ToString()))
                        LogDB.DeleteLogInfos(sb.ToString());
                    DoCmdEventQuery();
                }
            } catch (  Exception ex)
            {
                log.Error(ex);
            }            
        }
    }
}
